/*
 *新建分组
 */
package address;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author Mavis
 */
public class NewGroup extends JFrame {

    JPanel p = new JPanel();
    JLabel label = new JLabel("请输入新分组的名称");
    Font font = new Font("微软雅黑", Font.PLAIN, 18);
    Font font2 = new Font("微软雅黑", Font.PLAIN, 12);
    JButton jbtkeep ;
    JButton jbtcancel ;
    JTextField text = new JTextField();
    JLabel warn = new JLabel("*该分组已存在，请重新输入");

    NewGroup(ArrayList contacts, ArrayList belongs) {
        setLayout(null);
        
        ImageIcon Icon1 = new ImageIcon("img/confirm.png");    //btn 
        jbtkeep = new JButton(Icon1);
        jbtkeep.setBorderPainted(false);
        ImageIcon Icon2 = new ImageIcon("img/cancel_newPeople.png");    //btn 
        jbtcancel = new JButton(Icon2);
        jbtcancel.setBorderPainted(false);
        
        p.setLayout(null);
        p.setBounds(0, 0, 300, 220);
        p.setBackground(Color.white);
        add(p);

        label.setFont(font);
        label.setBounds(55, 20, 200, 30);
        p.add(label);

        text.setBackground(Color.white);
        text.setBounds(37, 65, 210, 30);
        p.add(text);

        warn.setBounds(38, 90, 200, 30);
        warn.setFont(font2);
        warn.setVisible(false);
        p.add(warn);

        jbtkeep.setFont(font);
        jbtkeep.setBounds(25, 125, 100, 30);
        jbtkeep.setBackground(Color.white);
        jbtkeep.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                int i;
                String s = text.getText();
                if (s.length() != 0) {
                    for (i = 0; i < belongs.size(); i++) {
                        String s2 = (String) belongs.get(i);
                        if (s.compareTo(s2) == 0) {
                            break;
                        }

                    }
                    if (i == belongs.size()) {
                        belongs.add(s);
                        dispose();
                        BeginPanel frame = new BeginPanel(contacts, belongs);
                        frame.setSize(900, 700);
                        frame.setTitle("欢迎使用通讯录管理系统");
                        frame.setLocationRelativeTo(null);
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.setVisible(true);
                    } else {
                        warn.setVisible(true);
                    }
                }
            }

        });
        p.add(jbtkeep);

        jbtcancel.setFont(font);
        jbtcancel.setBounds(155, 125, 100, 30);
        jbtcancel.setBackground(Color.white);
        jbtcancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                dispose();
                BeginPanel frame = new BeginPanel(contacts, belongs);
                frame.setSize(900, 700);
                frame.setTitle("欢迎使用通讯录管理系统");
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }

        });
        p.add(jbtcancel);
    }
}
